﻿//-----------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 , EricHu. 
//-----------------------------------------------------------------


using System;
using System.Security.Cryptography;
using System.Text;

namespace RDIFramework.Utilities
{
    /// <summary>
    /// 加解密公共类
    /// 
    /// <author>
    ///		<name>EricHu</name>
    ///		<date>2011.07.15</date>
    /// </author>
    /// </summary>
    public class SecretHelper
    {
        #region public static bool CheckRegister() 检查注册码是否正确
        /// <summary>
        /// 检查注册码是否正确
        /// </summary>
        /// <returns>是否进行了注册</returns>
        public static bool CheckRegister()
        {
            bool returnValue = true;
            // if (BaseConfiguration.Instance.CustomerCompanyName.Length == 0)
            // {
            //     returnValue = false;
            // }
            // 先把软件的使用权限设置设置到2012-12月以前可使用。
            if (SystemInfo.NeedRegister)
            {
                if ((DateTime.Now.Year >= 2012) && (DateTime.Now.Month > 12))
                {
                    returnValue = false;
                }
            }
            // 检查注册码,以避免到处被别人复制.
            // 2012.03.18 EricHu 改进注册方式,让底层程序更安全一些
            //if (BaseConfiguration.Instance.RegisterKey.Equals(CodeChange(BaseConfiguration.Instance.DataBase + BaseConfiguration.Instance.CustomerCompanyName)))
            //{
            //    returnValue = true;
            //}
            return returnValue;
        }
        #endregion

        #region 256位AES加解密
        /// <summary>
        /// 256位AES加密
        /// </summary>
        /// <param name="toEncrypt"></param>
        /// <returns></returns>
        public static string AESEncrypt(string toEncrypt)
        {
            if (string.IsNullOrEmpty(toEncrypt.Trim()))
            {
                return string.Empty;
            }

            byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");//12345678901234567890123456789012 注意，此处不能乱改动
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// 256位AES解密
        /// </summary>
        /// <param name="toDecrypt"></param>
        /// <returns></returns>
        public static string AESDecrypt(string toDecrypt)
        {
            if (string.IsNullOrEmpty(toDecrypt.Trim()))
            {
                return string.Empty;
            }

            byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
        #endregion
    }
}